import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

import createPersistedstate from 'vuex-persistedstate'

Vue.use(Vuex)

export default new Vuex.Store({
  plugins: [createPersistedstate()],
  state: {
    // token: localStorage.getItem('token') || ''
    token: '',
    userInfo: {}
  },
  mutations: {
    updateToken (state, token) {
      state.token = token
      //   同时存入localStorage
      //   不用存储 由插件帮我们完成
      //   localStorage.setItem('token', token)
    },
    updateUserInfo (state, userInfo) {
      state.userInfo = userInfo
    }
  },
  actions: {
    // async getUserInfo (context) {
    //   const { data: res } = await axios.get('/my/userInfo', {
    //     headers: {
    //       Authorization: context.state.token
    //     }
    //   })
    //   if (res.code === 0) {
    //     context.commit('updateUserInfo', res.data)
    //   }
    // }
    async getUserInfo (context) {
      const { data: res } = await axios.get('/my/userInfo')
      if (res.code === 0) {
        context.commit('updateUserInfo', res.data)
      }
    }
  }
})
